import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '_views/Home'
import { isLogin } from '_utils'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      isLogin: false,
      keepAlived: true
    }
  },
  {
    path: '/user',
    meta: {
      isLogin: true
    },
    component: () => import('_views/User')
  },
  {
    path: '/cate',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Cate')
  },
  {
    path: '/cart',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Cart')
  },
  {
    path: '/surprise/:id',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Surprise')
  },
  {
    path: '/detail',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Detail')
  },
  {
    path: '/itemlist',
    meta: {
      isLogin: false
    },
    component: () => import('_views/ItemList')
  },
  {
    path: '/login',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Login')
  },
  {
    path: '/regist',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Regist')
  },
  {
    path: '/search',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Search')
  },
  {
    path: '/searchlist',
    meta: {
      isLogin: false
    },
    component: () => import('_views/Searchlist')
  },
  {
    path: '/Addresslist',
    meta: {
      isLogin: true
    },
    component: () => import('_views/Addresslist')
  },
  {
    path: '/Addresscreate',
    meta: {
      isLogin: true
    },
    component: () => import('_views/Addresscreate')
  },
  {
    path: '/Addressupdate',
    meta: {
      isLogin: true
    },
    component: () => import('_views/Addressupdate')
  },
  {
    path: '/order',
    meta: {
      isLogin: true
    },
    component: () => import('_views/Order')
  },
  {
    path: '/userdetail',
    meta: {
      isLogin: true
    },
    component: () => import('_views/Userdetail')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    if (isLogin()) {
      next()
    } else {
      router.push({
        path: '/login',
        query: {
          from: to.path
        }

      })
      // next({
      //   path: '/login'
      //   query: {
      //   from: to.path
      //  }
      // })
    }
  } else {
    next()
  }
})
export default router
